layui.use('layer', function () {
    //由于layer弹层依赖jQuery，所以可以直接得到
    var $ = layui.$,
        layer = layui.layer;
    layui.use('element', function () {
        var element = layui.element; //导航的hover效果、二级菜单等功能，需要依赖element模块
    });

    // 设置弹窗登录框
    layui.use('layer', function () { //独立版的layer无需执行这一句
        var $ = layui.jquery,
            layer = layui.layer; //独立版的layer无需执行这一句

    });
    //表单
    layui.use('form', function () {
        var form = layui.form;

        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });
    // 收藏事件
    $(".status").find("a").click(function () {
        if ($(this).find("i").hasClass("layui-icon-star")) {
            $(this).find("i").removeClass("layui-icon-star");
            $(this).find("i").addClass("layui-icon-star-fill");
            $(this).find("i").css("color", "#FFD700");
            setCollection($(this));
        } else {
            $(this).find("i").removeClass("layui-icon-star-fill");
            $(this).find("i").addClass("layui-icon-star");
            $(this).find("i").css("color", "#000");
            delCollection($(this));
        }
    });
    if ($("#iscoled").val() == "true") {
        checkCol();
    }

    function checkCol() {
        $(".status").find("a").find("i").hasClass("layui-icon-star")
        $(".status").find("a").find("i").removeClass("layui-icon-star");
        $(".status").find("a").find("i").addClass("layui-icon-star-fill");
        $(".status").find("a").find("i").css("color", "#FFD700");
    }

    $(".setcol").click(function () {
        setCollection($(this));
        checkCol();
    });
    $(".setzan").click(function () {
        setzan($(this));
        $(this).find("i").css("color", "red");
    });
    setzan = function (obj) {
        var bid = $(obj).attr("value");
        $.ajax({
            url: '/Blog/setzan/' + bid,
            success: function (date) {
                layer.msg(date.msg);
            }
        });
    };
    setCollection = function (obj) {
        var bid = $(obj).attr("value");
        $.ajax({
            url: '/Blog/setcollection/' + bid,
            success: function (date) {
                layer.msg(date.msg);
            }
        })
    };
    delCollection = function (obj) {
        var bid = $(obj).attr("value");
        $.ajax({
            url: '/Blog/delcollection/' + bid,
            success: function (date) {
                layer.msg(date.msg);
            }
        });
    };

    // 举报事件
    $(".report").hover(function () {
        $(this).css("background-color", "white");
    });

    $(".report").hover(function () {
        $(".report-box").stop().fadeToggle();
    });

    //评论计数
    $("#area").bind("input propertychange", function () {
        var num = 1000 - $(this).val().length;
        $(".comment-status").find("em").html(num);
    });
    $("#area").click(function (e) {
        // $(".blog-labels ").show();
        e.stopPropagation();
        if ($(".comment-status").is(":hidden")) {
            $(".comment-status ").fadeIn("slow");
        } else {
            $(".comment-status").fadeOut("slow");
        }
        $(document).click(function () {
            if ($(".comment-status").is(":visible")) {
                $(".comment-status").fadeOut("slow");
            }
        });
    });
    $("#pulish").click(function () {
        $(".comment-status").fadeOut("slow");
        var content = $("#area").val();
        var bid = $(this).attr("value");
        if ($(this).text() == "发表评论") {
            $.ajax({
                url: '/Blog/setcom/' + bid,
                data: {bid: bid, content: content},
                success(data) {
                    layer.msg(data.msg);
                    if (data.msg == "评论成功!") {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    }
                }
            });
        } else if ($(this).text() == "回复") {
            $.ajax({
                url: '/Blog/setreply/' + bid,
                data: {bid: bid, content: content},
                success(data) {
                    layer.msg(data.msg);
                    if (data.msg == "回复成功!") {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    }
                }
            });
        } else {
            $.ajax({
                url: '/Blog/setRreply/' + bid,
                data: {bid: bid, content: content},
                success(data) {
                    layer.msg(data.msg);
                    if (data.msg == "回复成功!") {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000)
                    }
                }
            });
        }
    });
    $("#empty").click(function () {
        $("#area").val("");
        $(".comment-status").find("em").html(1000);
        $("#empty").hide("slow");
    });

    // 评论时间
    // var mydate = new Date();
    // var t = mydate.toLocaleString();
    // alert(t);
    // $("#time").text(t);

    //评论的操作
    $(".blog-comment-item").hover(function () {
        $(this).find(".item-ul").stop().fadeToggle();
    });
    $(".item-status").css("color", "#FF0000");

    //评论的回复
    var respan = $(".recomment").find(".blog-comment-item").find("span");
    respan.eq(1).css("color", "#999");
    respan.eq(3).css("color", "#999");
    // respan.eq(3).text(t);
    //回复评论
    reply = function (obj) {
        var iid = $(obj).attr("value");
        var cid = $(obj).parent().parent().attr("value");
        $("#empty").show("slow");
        $("html, body").animate({
            scrollTop: $($("#commentBox")).offset().top + "px"
        }, {
            duration: 300,
            easing: "swing"
        });
        $("#area").attr("placeholder", "回复 " + iid + " :");
        $("#pulish").text("回复");
        $("#pulish").val(cid);
        return false;
    };
    //回复回复
    Rreply = function (obj) {
        var iid = $(obj).attr("value");
        var cid = $(obj).parent().parent().attr("value");
        $("#empty").show("slow");
        $("html, body").animate({
            scrollTop: $($("#commentBox")).offset().top + "px"
        }, {
            duration: 300,
            easing: "swing"
        });
        $("#area").attr("placeholder", "回复 " + iid + " :");
        $("#pulish").text("回 复");
        $("#pulish").val(cid);
        return false;
    };
    //查看回复-收起回复
    // var comlist=$(".blog-comment-items").children("li");

    // var open=$(".blog-comment-item").find(".item-ul").children("li");
    // open.eq(2).click(function(){
    // 	$(this).hide();
    // 	$(".recomment").show("slow");
    // 	open.eq(3).show();
    // });
    // open.eq(3).click(function(){
    // 	$(this).hide();
    // 	$(".recomment").hide("slow");
    // 	open.eq(2).show();
    // });

    //评论点赞
    addzan = function (obj) {
        var cid = $(obj).parent().parent().attr("value");
        $.ajax({
            url: '/Blog/comzan/' + cid,
            success(data) {
                $(obj).text(data);
                $(obj).css("color", "#FF0000");
            }
        });
    };
    //删除评论
    delcom = function (obj) {
        var cid = $(obj).parent().parent().attr("value");
        $.ajax({
            url: '/Blog/delcom/' + cid,
            success(data) {
                if (data.msg == "删除成功!") {
                    $(obj).parent().parent().parent().hide("slow");
                }
                layer.msg(data.msg);
            }
        });
    };
    //查看回复
    getreply = function (obj) {
        var cid = $(obj).val();
        $(".recomment").hide();
        $(".blog-comment-items").children("li").each(function () {
            var opennum = $(this).find(".item-ul").children("li");
            opennum.eq(2).show();
            opennum.eq(3).hide();
        });
        $.ajax({
            url: '/Blog/getreply/' + cid,
            success(data) {
                $(".recomment .recomment-ul").empty();
                for (var o in data.replys) {
                    $(".recomment .recomment-ul").append(
                        "<li><div class='blog-comment-item' value=" + data.replys[o]["rid"] + ">" +
                        "<a href='/User/userexh/" + data.rusers[o]["uid"] + "'>" +
                        "<img src=" + data.rusers[o]["img"] + "/>" +
                        "<span >" + data.rusers[o]["uid"] + "</span></a>" +
                        "<span>回复</span>" +
                        "<a href='/User/userexh/" + data.replys[o]["iid"] + "'>" +
                        "<span>" + data.replys[o]["iid"] + "</span></a>" +
                        "<span>" + data.replys[o]["date"] + "</span>" +
                        "" +
                        "<div class='item_status'>" +
                        "<span class='layui-icon layui-icon-praise' onclick='repaddzan(this);'>" + data.replys[o]["likeNumber"] + "</span>" +
                        "</div>" +
                        "<ul class='re item-ul'>" +
                        "<li onclick='delrep(this);'value=" + data.replys[o]["rid"] + ">删除</li>" +
                        "<li onclick='Rreply(this);' value=" + data.rusers[o]["uid"] + ">回复</li></ul>" +
                        "<div class='blog-comment-item-content'>" +
                        "<span>" + data.replys[o]["content"] + "</span>" +
                        "</div></div></li>"
                    );
                }

                $(obj).hide();
                $(obj).parents("li").find(".recomment").show("slow");
                $(obj).next().show();
                //append后回复的hover事件
                $(".blog-comment-item").hover(function () {
                    $(this).find(".re").stop().fadeToggle();
                });
                //改变回复点赞颜色
                var opennum = $(".recomment .recomment-ul").children("li");
                opennum.each(function () {
                    changecol($(this).find(".item_status").find("span"));
                });
                // if($(".item_status").find("span"));
            }
        });
    };
    //删除回复
    delrep = function (obj) {
        var rid = $(obj).attr("value");
        $.ajax({
            url: '/Blog/delreply/' + rid,
            success(data) {
                if (data.msg == "删除成功!") {
                    $(obj).parent().parent().parent().hide("slow");
                }
                layer.msg(data.msg);
            }
        });
    };
    //回复点赞
    repaddzan = function (obj) {
        var rid = $(obj).parent().parent().attr("value");
        $.ajax({
            url: '/Blog/repaddzan/' + rid,
            success(data) {
                $(obj).text(data);
                $(obj).css("color", "#FF0000");
            }
        });
    };
    $(".blog-comment-items").children("li").each(function () {
        //改变评价点赞颜色
        changecol($(this).find(".item_status").find("span"));
        var fa = $(this);
        var open = $(this).find(".blog-comment-item").find(".item-ul").children("li");
        var opennum = fa.find(".recomment").find(".recomment-ul").children("li");
        // open.eq(2).html("查看回复(" + opennum.length + ")");
        open.eq(2).html("查看回复");
        // open.eq(2).click(function () {
        //     $(this).hide();
        //     fa.find(".recomment").show("slow");
        //     open.eq(3).show();
        // });
        open.eq(3).click(function () {
            $(this).hide();
            fa.find(".recomment").hide("slow");
            open.eq(2).show();
        });
        //改变回复点赞颜色
        opennum.each(function () {
            changecol($(this).find(".item_status").find("span"));
        });
    });

    //改变点赞颜色
    function changecol(co) {
        if (co.text() == 0) {
            co.css("color", "#000000");
        } else {
            co.css("color", "#FF0000");
        }
    }

    //推荐阅读数量
    $(".recommend-item").find("#readnum").text("阅读数:3");


    $(window).scroll(function () {
        //为了保证兼容性，这里取两个值，哪个有值取哪一个
        //scrollTop就是触发滚轮事件时滚轮的高度
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;

        var clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
        var scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;

        // console.log("滚动距离" + scrollTop);
        // 由于未知的原因,如果写在一起或导致页面从底部向上滚动过快时右侧导航栏无法触发向下移动,所以分开写

        // 右侧导航栏
        var height = scrollHeight - clientHeight;
        if (scrollTop < 100) {
            $(".right-Navigation").stop().animate({
                top: '90px'
            }, 400);
        } else if (scrollTop > 100) {
            $(".right-Navigation").stop().animate({
                top: '30px'
            }, 400);
        }

        //当页面向下滚动到遮住轮播图是，列表向上移动
        // if ( scrollTop < 300) {
        // 	$(".b").stop().animate({
        // 		top: '0'
        // 	}, 400);
        // }else if (scrollTop > 300) {
        // 	$(".b").stop().animate({
        // 		top: '-100px'
        // 	}, 400);
        // }
    });
    $(".toComment").click(function () {
        $("html, body").animate({
            scrollTop: $($(this).attr("href")).offset().top + "px"
        }, {
            duration: 300,
            easing: "swing"
        });
        return false;
    });
    $(".totop").click(function () {
        timer = setInterval(function () {
            //获取滚动条的滚动高度
            var osTop = document.documentElement.scrollTop || document.body.scrollTop;
            //用于设置速度差，产生缓动的效果
            var speed = Math.floor(-osTop / 6);
            document.documentElement.scrollTop = document.body.scrollTop = osTop + speed;
            isTop = true; //用于阻止滚动事件清除定时器
            if (osTop == 0) {
                clearInterval(timer);
            }
        }, 30);
    });

    // 让博客标签片自适应高度
    // iFrameHeight();
    //
    // function iFrameHeight() {
    // 	var ifm = document.getElementById("blogTipsFrame");
    // 	var subWeb = document.frames ? document.frames["blogTipsFrame"].document : ifm.contentDocument;
    // 	if (ifm != null && subWeb != null) {
    // 		ifm.style.height = 'auto'; //关键这一句，先取消掉之前iframe设置的高度
    // 		ifm.style.height = subWeb.body.scrollHeight + 'px';
    // 	}
    // };

});
